package com.neusoft.yiyang.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class CustomStreamFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 获取响应对象
        ServerHttpResponse response = exchange.getResponse();
        
        // 针对/yiyang/chat接口禁用响应缓冲
        if (exchange.getRequest().getPath().value().contains("/yiyang/chat")) {
            // 禁用响应缓冲（关键配置）
            response.getHeaders().set("Cache-Control", "no-cache, no-store, must-revalidate");
            response.getHeaders().set("Pragma", "no-cache");
            response.getHeaders().set("Expires", "0");
        }
        
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return -100; // 设置过滤器执行顺序
    }
}